﻿using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Zhao.Arena.Utility.InitDatabaseExt;
using Zhao.Arena.Utility.SwaggerExt;
using Zhao.Common.EnumEntity;
using Zhao.Common.Helper;
using Zhao.IServer.User;
using Zhao.Models.DTO;
using Zhao.Models.Entity;
using Zhao.Service.User;

namespace Zhao.Arena.Controllers.User
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    [Route("api/[controller]")]
    [ApiController]
    [Function("用户管理", null, "user", "../views/Home/user/index.vue")]
    [Authorize]
    public class UserController : ControllerBase
    {
        private readonly IUsersServer _usersServer;
        private readonly IMapper _mapper;
        public UserController(IUsersServer usersServer, IMapper mapper)
        {
            _usersServer = usersServer;
            _mapper = mapper;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="searchaString"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{pageIndex:int}/{pageSize:int}")]
        [Route("{pageIndex:int}/{pageSize:int}/{searchaString}")]
        [Function("用户详情", null, "user", "../views/Home/user/userInfo/index.vue")]
        public async Task<JsonResult> GetUserPage(int pageIndex = 1, int pageSize = 10, string searchaString = " ")
        {
            Expressionable<Users> expressionable = new Expressionable<Users>();

            var result = new ApiResult<PageModel<UsersDto>>() { Success = false, Message = "操作失败" };

            expressionable.AndIF(!searchaString.IsNullOrWhiteSpace(), u => u.Username.Contains(searchaString) || u.Phone.Contains(searchaString));

            var data = await _usersServer.QueryPageAsync(expressionable.ToExpression(), null, pageIndex, pageSize);

            result.Data = _mapper.Map<PageModel<UsersDto>>(data);

            if (result.Data != null)
            {
                result.Success = true;
                result.Message = "操作成功";
            }

            return new JsonResult(result);
        }


        /// <summary>
        /// 切换用户账户状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("status/{id:int}")]
        public async Task<JsonResult> UserFrozen(int id)
        {
            var result = new ApiResult()
            {
                Message = "失败",
                Success=false
            };

            var user = await _usersServer.QueryAsync(id);
            if (user == null)
            {
                result.Message = "角色不存在";
                return new JsonResult(result);
            }

            //切换用户状态
            if(user.Status==(int)UserStatusEnum.Normal) user.Status= (int)UserStatusEnum.Frozen;
            else user.Status = (int)UserStatusEnum.Normal;

            var success = await _usersServer.UpdateAsync(user);

            result.Success = success;
            result.Message = success ? "状态更新成功" : "状态更新失败";

            return new JsonResult(result);

        }



    }
}
